package com.sky.zuor;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import com.sky.zuor.ent.FundsInfoResponse;
import lombok.Data;
import okhttp3.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class ZuiruoAssets {
    public static final String base = "https://openapi8.zr66.com";

    public CashFlowResponse queryCashFlow(CashFlowRequestV2VO requestVO, String token) throws IOException {

        Gson gson = new GsonBuilder().create();
        // 创建请求
        Request request = new Request.Builder()
                .url(base + "/funds/get_cash_flow") // 替换为实际的服务器地址
                .addHeader("token", token)
                .post(RequestBody.create(gson.toJson(requestVO), MediaType.parse("application/json")))
                .build();

        OkHttpClient client = new OkHttpClient();
        // 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            // 处理响应
            String responseData = response.body().string();
            System.out.println("Response: " + responseData);

            // 将响应解析为 CashFlowResponse 对象
            return gson.fromJson(responseData, CashFlowResponse.class);
        }
    }

    @Data
    public static class CashFlowRequestV2VO {
        private String startDate; // 起始查询时间
        private String endDate;   // 截止查询时间
        private String moneyType; // 币种类别

        // Getters and Setters
    }

    @Data
    public class CashFlowResponse {
        private String code; // 错误码
        private String msg;  // 异常描述
        private List<CashFlowRecord> data; // 资金流水记录

        // Getters and Setters
    }

    @Data
    public class CashFlowRecord {
        private String currDatetime;  // 实际日期时间
        private double occurBalance;   // 发生金额
        private String moneyType;      // 币种类别
        private String code;           // 业务标志

        // Getters and Setters
    }


    public HoldListResponse queryHoldList(HoldListVO requestVO, String token) throws IOException {
        Gson gson = new GsonBuilder().create();
        // 创建请求
        Request request = new Request.Builder()
                .url(base + "/funds/get_hold_list") // 替换为实际的服务器地址
                .addHeader("token", token)
                .post(RequestBody.create(gson.toJson(requestVO), MediaType.parse("application/json")))
                .build();

        OkHttpClient client = new OkHttpClient();
        // 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            // 处理响应
            String responseData = response.body().string();
            System.out.println("持仓响应Response: " + responseData);

            // 将响应解析为 HoldListResponse 对象
            return gson.fromJson(responseData, HoldListResponse.class);
        }
    }

    @Data
    public static class HoldListVO {
        private Integer market; // 交易市场

        // Getters and Setters
    }

    @Data
    public class HoldListResponse {
        private String code; // 错误码
        private String msg;  // 异常描述
        private HoldListResponseData data;
        // Getters and Setters
    }

    @Data
    public class HoldListResponseData {
        private CashAmt cashAmt;  // 三大币种现金
        private List<HoldRecord> holdList; // 持仓列表
    }

    @Data
    public class CashAmt {
        private double cnCashAmt; // 人民币现金
        private double hkCashAmt; // 港币现金
        private double usCashAmt; // 美币现金
    }

    @Data
    public class HoldRecord {
        private String code; // 证券代码
        private double costPrice; // 成本价
        private double currentAmount; // 持有数量
        private double enableAmount; // 可卖数量
        private double last; // 现价
        private Integer market; // 股票市场
        private String name; // 证券名称
        private Integer suspension; // 停牌标识
        private String ts; // 交易市场
        private Integer type; // 证券类型

        // Getters and Setters
    }


    public MarginRatioResponse getStockMarginRatio(QueryMarginRatioVO requestVO, String token) throws IOException {
        Gson gson = new GsonBuilder().create();
        // 创建请求
        Request request = new Request.Builder()
                .url(base + "/funds/get_stock_margin_ratio") // 替换为实际的服务器地址
                .addHeader("token", token)
                .post(RequestBody.create(gson.toJson(requestVO), MediaType.parse("application/json")))
                .build();

        OkHttpClient client = new OkHttpClient();
        // 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            // 处理响应
            String responseData = response.body().string();
            System.out.println("Response: " + responseData);

            // 将响应解析为 MarginRatioResponse 对象
            return gson.fromJson(responseData, MarginRatioResponse.class);
        }
    }

    @Data
    public static class QueryMarginRatioVO {
        private String code; // 股票代码
        private String ts;   // 股票市场

        // Getters and Setters
    }

    @Data
    public class MarginRatioResponse {
        private String code; // 错误码
        private String msg;  // 异常描述
        private Double marginRatio; // 孖展比例 (融资比例)

        // Getters and Setters
    }


    public static FundsInfoResponse getFundsInfo(String token) throws IOException {
        // 创建请求
        Request request = new Request.Builder()
                .url(base + "/funds/info") // 替换为实际的服务器地址
                .post(RequestBody.create("", MediaType.parse("application/json"))) // POST请求体为空
                .addHeader("token", token) // 添加语言参数到请求头
                .build();
        OkHttpClient client = new OkHttpClient();
        // 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            // 处理响应
            String responseData = response.body().string();
            System.out.println("Response: " + responseData);
            Gson gson = new GsonBuilder().create();
            // 将响应解析为 FundsInfoResponse 对象
            return gson.fromJson(responseData, FundsInfoResponse.class);
        }
    }


    public ExchangeRateResponse getMoneyTypeList(String token) throws IOException {
        // 创建请求
        Request request = new Request.Builder()
                .url(base + "/funds/money_type_list") // 替换为实际的服务器地址
                .addHeader("token", token) // 添加语言参数到请求头
                .post(RequestBody.create("", MediaType.parse("application/json"))) // 发送空请求体
                .build();
        OkHttpClient client = new OkHttpClient();
        // 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }

            // 处理响应
            String responseData = response.body().string();
            System.out.println("Response: " + responseData);
            Gson gson = new GsonBuilder().create();
            // 将响应解析为 ExchangeRateResponse 对象
            return gson.fromJson(responseData, ExchangeRateResponse.class);
        }
    }


    @Data
    public class ExchangeRateResponse {
        private String code; // 错误码
        private String msg;  // 异常描述
        private List<ExchangeRateData> data; // 汇率数据
    }

    @Data
    public static class ExchangeRateData {
        private String moneyType; // 货币类型
        private Double rate;       // 汇率
        private String toMoneyType; // 目标币种类别
        // Getters and Setters
    }

}
